package de.zainodis.triplog.data.persistence;

import android.content.Context;

import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.support.ConnectionSource;

/**
 * Helper class for database related test-cases.
 * Created by fzarrai on 9/12/2014.
 */
public class HelperProvider {
    protected DatabaseHelper databaseHelper;

    public DatabaseHelper getHelper(Context context) {
        if (databaseHelper == null) {
            databaseHelper = OpenHelperManager.getHelper(context, DatabaseHelper.class);
        }
        return databaseHelper;
    }

    public void releaseHelper() {
        if (databaseHelper != null) {
            OpenHelperManager.releaseHelper();
            databaseHelper = null;
        }
    }

    protected ConnectionSource getConnectionSource(Context context) {
        return getHelper(context).getConnectionSource();
    }

    @Override
    protected void finalize() throws Throwable {
        // Auto release obtained helper on object destruction
        releaseHelper();
        super.finalize();
    }
}
